/*
  EOMono
  Copyright 2007, Adam Tauno Williams (Whitemice Consulting)
  License: LGPLv2  < http://www.gnu.org/licenses/lgpl-2.1.html >
 */
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

namespace Whitemice.EOMono
{
  [EOCommandAttribute("SQLFetch", 1, "Fetch from the specified entity.")]
  public class EOSQLFetchCommand : EOSQLCommand, IEOCommand
  {
    protected string   qualifier   = null;
    protected object[] parameters  = null;
    protected object[] criteria    = null;
    protected string   conjunction = "AND";
    protected string   expression  = "EQUALS";
    
    protected EORecordReceiver  receiver = null;
  
    public EOSQLFetchCommand(IEOConnection connection, string entity) :
      base(connection, entity)
    {
    } // end ctor
    
    public EOSQLFetchCommand(IEOConnection connection, EOEntity entity) :
      base(connection, entity)
    {
    } // end ctor
    
    [EOArguementAttribute("receiver", "")]
    public EORecordReceiver Receiver
    {
      set { this.receiver = value; }
    } // end Receiver
    
    [EOArguementAttribute("parameters", "")]
    public object[] Parameters
    {
      set { parameters = value; }
      get { return parameters; }
    } // end Parameters
    
    [EOArguementAttribute("qualifier", "")]
    public string Qualifier
    {
      set { qualifier = value; }
      get { return qualifier; }
    } // end Qualifier
    
    [EOArguementAttribute("criteria", "")]
    public object[] Criteria
    {
      set { criteria = value; }
      get { return criteria; }
    } // end Criteria
    
    [EOArguementAttribute("conjunction", "")]
    public string Conjunction
    {
      get { return conjunction; }
      set { conjunction = value; }
    } // end Conjunction
    
    [EOArguementAttribute("expression", "")]
    public string Expression
    {
      get { return expression; }
      set { expression = value; }
    }
    
    // TODO: Use StringBuilder
    protected void QualiferFromCriteria()
    {
      int      count = 0;

      if (Criteria != null)
      {
        Qualifier = string.Empty;
        while (count < Criteria.Length)
        {
          string    f;
          object    v;
          string    e;
          
          f = Criteria[count++].ToString();
          v = Criteria[count++];
          if (count > 2)
          {
            Qualifier = string.Format("{0} {1} ",
              Qualifier, 
              Conjunction);
          }
          e = FormatExpression(Entity, Entity.GetField(f).Name, Expression, v);
          Qualifier = string.Format("{0} {1} ", Qualifier, e);
        } // end while
      } else Qualifier = " 1 = 1";
    } // end QualiferFromCriteria 
    
    /// <summary>
    /// Execute fetch command
    /// </summary>
    protected override void Execute()
    {
      string sql;
      
      if (Entity == null)
        throw new EONoEntityException();
      if (Qualifier == null)
        QualiferFromCriteria();
      if (Parameters != null)
        foreach (object param in Parameters)
          Entity.Parameters.Add(param);
      sql = string.Format("SELECT {0} FROM {1} WHERE {2}",
                          Entity.GetFieldsAsCSV(),
                          Entity.InternalName,
                          Qualifier);
      EOManager.Log.DebugFormat("SQL:{0}", sql);
      command.CommandText = sql;
      ReadToResults(command.ExecuteReader(), receiver);
      Entity.Parameters.Clear();
    } // end Execute
  } // end class
}
